﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FeCMS;
using System.IO;
using FeCMS.DataSource;
using FeCMS;
using FeCMS.AccessControlModule;
using FeCMS.TemplateEngine;
using FeCMS.TemplateEngine.DataSource;

namespace TemplateTestTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        #region 私有变量

        /// <summary>
        /// 用于记录当前的层数
        /// </summary>
        private int LebleLevel = 0;


        #endregion


        /// <summary>
        /// 切换测试文本
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void contentTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            var str_1 = @"<form id=""delbox"">
    <fieldset id=""personal"">  {Fe:QueryStr key=""Controller""  mykey=""Controller""/}
      <legend>是否删除该数据</legend>{Fe:Qttt key=""Controller""/}ABCDE";

            var str_2 = @"<ul>
      {Fe:Loop key=""Controller""  mykey=""Controller""}
            <li>是否删除该数据</li>
      {/Fe:Loop}
</ul>
";


            var str_3 = @"<ul>
      {Fe:Loop key=""Controller""  mykey=""Controller""}
            <li>是否删除该数据</li>
      {/Fe:Loop}
</ul>
<div id=""delbox"">
   {Fe:Ctrl name=""box""/}
</div>
";


            var str_4 = @"<form id=""delbox"">
    <fieldset id=""personal"">  {Fe:QueryStr key=""Controller""  mykey=""Controller""/}
      <legend>是否删除该数据</legend>{Fe:Qttt key=""Controller""/}ABCDE";



            switch (contentTypeComboBox.Text)
            {
                case "单一标签解析":
                    templateContentTextBox.Text = str_1;
                    break;
                case "循环标签解析":
                    templateContentTextBox.Text = str_2;
                    break;
                case "混合标签解析":
                    templateContentTextBox.Text = str_3;
                    break;
                case "内标签解析":
                    templateContentTextBox.Text = str_4;
                    break;
            }
        }

        
        /// <summary>
        /// 开始测试
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void startBtn_Click(object sender, EventArgs e)
        {
            #region 数值的初始化

            LebleLevel = 0;
            ContentTextBox.Text = string.Empty;
            resultOfLableNameTextBox.Text = string.Empty;
            resultOfLablePropertyTextBox.Text = string.Empty;
            resultOfLableContentTextBox.Text = string.Empty;
            #endregion


            var txt = templateContentTextBox.Text;
            TemplateReader template = new TemplateReader(new StringReader(txt));
            //解析内容
            template.ExplanationContentFun = ExplanationContentFun;
            //解析标签
            template.ExplanationLabelFun = ExplanationLabelFun;
            template.Read();
        }

        /// <summary>
        /// 解析内容部分
        /// </summary>
        /// <param name="val"></param>
        private void ExplanationContentFun(string val)
        {
            ContentTextBox.Text += string.Format("\r\nN:{0}=====================\r\n{1}", ++LebleLevel, val);
        }
        /// <summary>
        /// 解析标签部分
        /// </summary>
        /// <param name="labelName"></param>
        /// <param name="labelProperty"></param>
        /// <param name="labelValue"></param>
        private void ExplanationLabelFun(string labelName, Dictionary<string, string> labelProperty, string labelValue)
        {
            resultOfLableNameTextBox.Text += string.Format("\r\nN:{0}=====================\r\n{1}", ++LebleLevel, labelName);
            resultOfLablePropertyTextBox.Text += string.Format("\r\nN:{0}=====================\r\n", labelName);
            if (labelProperty != null)
            {
                foreach (var keyVal in labelProperty)
                {
                    resultOfLablePropertyTextBox.Text += string.Format("KEY:{0}====VAL:{1}\r\n", keyVal.Key, keyVal.Value);
                }
            }
            resultOfLableContentTextBox.Text += string.Format("\r\nN:{0}=====================\r\n{1}", LebleLevel, labelValue);
        }

        LableDataTree lableDataTree = new LableDataTree();
        /// <summary>
        /// 添加值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddKeyValBtn_Click(object sender, EventArgs e)
        {
            
            if(!string.IsNullOrEmpty(theKeyTextBox.Text) &&!string.IsNullOrEmpty(theValTextBox.Text) )
            {
                lableDataTree.AppendLableData(theKeyTextBox.Text,theValTextBox.Text);
                nowLevelTextBox.Text =lableDataTree.NowLevel.ToString();
                MessageBox.Show("添加成功！");
            }
            else
            {
                MessageBox.Show("请输入键跟值");
            }
        
        }

        /// <summary>
        /// 向下级添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddKeyValNextLevelBtn_Click(object sender, EventArgs e)
        {
        if(!string.IsNullOrEmpty(theKeyTextBox.Text) &&!string.IsNullOrEmpty(theValTextBox.Text) )
            {
                lableDataTree.AppendNextLevelLableData(theKeyTextBox.Text,theValTextBox.Text);
                nowLevelTextBox.Text =lableDataTree.NowLevel.ToString();
                MessageBox.Show("添加成功！");
            }
            else
            {
                MessageBox.Show("请输入键跟值");
            }
        }

        /// <summary>
        /// 查找键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SearchKeyValbtn_Click(object sender, EventArgs e)
        {
             if(!string.IsNullOrEmpty(searchKeyTextBox.Text))
             {
                 var result = lableDataTree.SearchLableData(searchKeyTextBox.Text);
                 if(result)
                 {
                     MessageBox.Show(string.Format("值为：{0}",lableDataTree.SearchResult));
                 }
                 else
                 {
                     MessageBox.Show("没有查找到值！");
                 }
             }
            else
             {
                 MessageBox.Show("请输入查询的键");
             }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //MyResourceManager<Admin> mg = new MyResourceManager<Admin>();
            //mg.Add("first1", new Admin()
            //{
            //    Password = "123131233",
            //    RoleID = new int[] { 1 },
            //    UserName = "001"
            //});

            //var V = mg.Get("first1");
            //if (V != null)
            //{
            //    MessageBox.Show(V.UserName);
            //}
            //else
            //{
            //    MessageBox.Show("空");
            //}
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //MyResourceManager<Admin> mg = new MyResourceManager<Admin>();
            //mg.Add("first2", new Admin()
            //{
            //    Password = "123131233",
            //    RoleID = new int[] { 1 },
            //    UserName = "002"
            //});

            //var V = mg.Get("first2");
            //if (V != null)
            //{
            //    MessageBox.Show(V.UserName);
            //}
            //else
            //{
            //    MessageBox.Show("空");
            //}
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //MyResourceManager<Admin> mg = new MyResourceManager<Admin>();

            //var V = mg.Get("first1");
            //if (V != null)
            //{
            //    MessageBox.Show(V.UserName);
            //}
            //else
            //{
            //    MessageBox.Show("空");
            //}
            //V = mg.Get("first2");
            //if (V != null)
            //{
            //    MessageBox.Show(V.UserName);
            //}
            //else
            //{
            //    MessageBox.Show("空");
            //}
        }
    }
}
